package com.chinasoft.maysham.manager.repo.project;

import com.chinasoft.maysham.manager.model.Project;
import com.chinasoft.maysham.manager.model.ProjectUser;
import com.chinasoft.maysham.manager.repo.BaseRepo;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class ProjectRepo extends BaseRepo {

    public boolean exist(Criteria criteria) {
        return exist(criteria, Project.class);
    }

    public List<Project> listBy(String userId, int page, int size) {
        List<String> projectIds = template.findDistinct(Query.query(Criteria.where("userId").is(userId).and("deleted").is(false)), "projectId",
                ProjectUser.class, String.class);
        return template.find(Query.query(Criteria.where("deleted").is(false).and("id").in(projectIds))
                .with(PageRequest.of(page, size)).with(Sort.by(Sort.Direction.DESC, "createdOn")), Project.class);
    }
}
